/**
 * @Author 12629
 * @Description：
 */
public class HashBuck2<K,V> {

    public static class Node<K,V> {
        public K key;
        public V val;
        public Node<K,V> next;

        public Node(K key, V val) {
            this.key = key;
            this.val = val;
        }
    }

    public Node<K,V>[] array
            = (Node<K,V>[])new Node[10];

    //查字典的逻辑 美女  美-》{美景，美食，美丽，美女}
    public void put(K key,V val) {
        //1. 通过哈希函数 找到位置
        int hashCode = key.hashCode();
        int index = hashCode % array.length;

        //2.判断当前数组下标的链表 有没有Key相同的，如果有 更新val
        Node<K,V> cur = array[index];
        while (cur != null) {
            if(cur.key.equals(key)) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
    }

    public V get(K key) {
        //1. 通过哈希函数 找到位置
        int hashCode = key.hashCode();
        int index = hashCode % array.length;
        Node<K,V> cur = array[index];
        while (cur != null) {
            if(cur.key.equals(key)) {
                return cur.val;
            }
            cur = cur.next;
        }
        return null;
    }
}
